import java.util.HashMap;
import java.util.ArrayList;
import java.util.Set;
import java.util.LinkedHashSet;
/**
 * The database of all the users
 * 
 * @author Mathieu Reymond & Jonathan Smet 
 * @version 03/05/11
 */
public abstract class UserData
{
    private static HashMap<String, User> userData = new HashMap<String, User>();    

    public UserData()
    {
    }
    
       /**
     * The methode add a new user to the database
     * 
     * @param userToAdd The user that you want to add.
     */
      public static void addUser(User userToAdd)
    {
        userData.put(userToAdd.getName(), userToAdd);
    }
    
        /**
     * Delete the user based on the user-object you want to delete. 
     * 
     * @param userToDelete The user you want to delete
     */
    public static void deleteUser(User userToDelete)
    {
        userData.remove(userToDelete.getName());
    }
    
        /**
     * Delete the user based on his name 
     * 
     * @param nameOfUserToDelete The name of the user that has to be deleted
     */
    public static void deleteUser(String nameOfUserToDelete)
    {
        userData.remove(nameOfUserToDelete);
    }
    
        /**
     * Returns a list of all the names of the users in the database
     * 
     * @return A list of the names of the users in the database
     */
    public static String[] getNameList()
    {
        ArrayList<String> nameList = new ArrayList<String>();
        Set<String> keySet = userData.keySet();
        Object keyObject[] = keySet.toArray();
        String keyList[] = new String[keySet.size()];
        for(int index = 0; index < keyList.length; index++)
        {
            keyList[index] = keyObject[index].toString();
        }
        return keyList;
    }
    
    public static HashMap<String, User> getUserData()
    {
        return userData;
    }
    /**
     * This method will look for every word in the String and see if it matches with any word of the
     * name of any object in the database
     * 
     * @param searchInput The key-words that need to be searched
     */
    public static LinkedHashSet<String> SearchUser(String searchInput)
    {
        String hitWord;

        String searchSplit[] = searchInput.split(" ");
        String match;
                
        int t=0;
        int i=0;
        int z=0;
        
        
        LinkedHashSet<String> searchUserResults= new LinkedHashSet <String>();

        while (t<15 &&  z<searchSplit.length)
        {
            hitWord = searchSplit[z].toLowerCase();
            while (i< getNameList().length) 
            {
                match = getNameList()[i].toLowerCase();
                if (match.contains(hitWord))
                {
                            searchUserResults.add(getNameList()[i]);
                            t++;                            
                }
                i++;                
           }
           z++;
        }
        return searchUserResults;
    }
    
        /**
     * This method gives a list with all the comments of a certain user
     * 
     * @param The user that wrote the comments
     * @return The arraylist filled with the comments of 1 user
     */
    public static ArrayList<Comment> getUserComments(User user)
    {
        ArrayList<Comment> userComments = new ArrayList<Comment>();
        for(int index = 0; index < LineUp.getNameList().length; index++)
        {
            ArrayList<Comment> commentsToCheck = LineUp.getLineUp().get(LineUp.getNameList()[index]).getComments();
            
            for(int i = 0; i < commentsToCheck.size(); i++)
            {
                if(user.getName().equals(commentsToCheck.get(i).getUserName()))
                {
                    userComments.add(commentsToCheck.get(i));
                }
            }
        }
        return userComments;
    }
    
}
